package com.sdy.healthcode.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.Response;
import com.sdy.common.utils.DateUtil;
import com.sdy.healthcode.biz.constants.HealthCodeType;
import com.sdy.healthcode.biz.constants.RedisConstants;
import com.sdy.healthcode.biz.dto.CodeCount;
import com.sdy.healthcode.biz.dto.CodeCountPack;
import com.sdy.healthcode.biz.dto.CommunityActionDTO;
import com.sdy.healthcode.biz.model.NcovArea;
import com.sdy.healthcode.biz.model.User;
import com.sdy.healthcode.biz.service.HealthCodeService;
import com.sdy.healthcode.biz.service.NcovAreaService;
import com.sdy.healthcode.biz.service.UserActionService;
import com.sdy.healthcode.biz.service.UserService;
import com.sdy.mvc.controller.BaseController;
import com.sdy.redis.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 新冠肺炎分布区域表 前端控制器
 * </p>
 *
 * @author cgj
 * @since 2020-02-25
 */
@Slf4j
@RestController
@RequestMapping("/ncovArea")
@Api(tags = "实时监控")
public class NcovAreaController extends BaseController {
    @Autowired
    private NcovAreaService ncovAreaService;
    @Autowired
    private HealthCodeService healthCodeService;
    @Autowired
    private UserActionService userActionService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private UserService userService;

    @GetMapping("/selectNcovArea")
    @ApiOperation(value = "测试",response = NcovArea.class)
    @ApiImplicitParam(value = "所选省份名称", name = "area")
    public Response selectNcovArea(String area, String code, Page page, HttpServletRequest request) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Date end = c.getTime();
        // 当前日期
        String today= format.format(end);
        c.add(Calendar.DATE, -1);
        Date start = c.getTime();
        // 前一天
        String yesterday= format.format(start);

        // 获取今日区域数据信息
        LambdaQueryWrapper<NcovArea> wrapperToday = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<NcovArea>  ncovAreaToday= wrapperToday.eq(NcovArea::getArea, area).eq(NcovArea::getStatisticDate, today);
        NcovArea oneToday = ncovAreaService.getOne(ncovAreaToday);
        // 获取昨日区域数据信息
        LambdaQueryWrapper<NcovArea> wrapperYesterday = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<NcovArea> ncovAreaYday = wrapperYesterday.eq(NcovArea::getArea, area).eq(NcovArea::getStatisticDate, yesterday);
        NcovArea oneYday = ncovAreaService.getOne(ncovAreaYday);

        if (oneYday != null && oneToday != null) {
            // 确诊较昨日
            int diagnosiscy = oneToday.getCuCoYday() - oneYday.getCuCoYday();
            // 死亡较昨日
            int deathcy = oneToday.getCuDeYday() - oneYday.getCuDeYday();
            // 治愈较昨日
            int curecy = oneToday.getCuCuYday() - oneYday.getCuCuYday();
            // --------------总体情况数据--------------------------
            oneToday.setDiagnosiscy(diagnosiscy).setDeathcy(deathcy).setCurecy(curecy);
        }

        // ----------------------健康码统计图-------------------
        List<CodeCountPack> m = new ArrayList<>();
        Date now = new Date();
        for (int i = 29; i > 0; i--) {
            String d  = DateUtil.formatDate(DateUtil.addDate(now, -i));
            CodeCountPack codeCountPack = redisService.getOrDefault(
                    RedisConstants.CODE_COLOUR_PREFIX + d + code,
                    CodeCountPack.class,
                    new CodeCountPack()
                            .setDate(d)
                            .setR(new CodeCount().setHcode(HealthCodeType.RED.getCode()).setCounts(0))
                            .setG(new CodeCount().setHcode(HealthCodeType.GREEN.getCode()).setCounts(0))
                            .setY(new CodeCount().setHcode(HealthCodeType.YELLOW.getCode()).setCounts(0)));
            m.add(codeCountPack);
        }
        CodeCountPack codeCountToday = healthCodeService.getCodeCount(code);
        m.add(codeCountToday.setDate(DateUtil.formatDate(now)));

        // ----------进出小区情况------------

        HashMap<Object, Object> param = new HashMap<>();
        param.put("populationParam", oneToday);
        param.put("codeCountParam", m);

        return Response.success(param);
    }

    @GetMapping("/communityActionPage")
    @ApiOperation(value = "进出小区情况分页")
    public Response communityActionPage(String code, Page<CommunityActionDTO> page) {
        // ----------进出小区情况------------
        IPage<CommunityActionDTO> communityActionDTOIPage = userActionService.pageCommunityAction(page, code);
        return Response.success(communityActionDTOIPage);
    }
}
